home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr57 / mentc.zip / FILEIO.C < prev    next >
C/C++ Source or Header  |  1993-06-01  |  6KB  |  263 lines

  1. /*  FILEIO.C:   Low level file i/o routines
  2.         MicroEMACS 3.10
  3.  
  4.  * The routines in this file read and write ASCII files from the disk. All of
  5.  * the knowledge about files are here.
  6.  */
  7.  
  8. #include        <stdio.h>
  9. #include    "estruct.h"
  10. #include    "eproto.h"
  11. #include        "edef.h"
  12. #include    "elang.h"
  13.  
  14. #if    AOSVS
  15. #define    fopen    xxfopen
  16. #endif
  17.  
  18. NOSHARE FILE *ffp;        /* File pointer, all functions. */
  19. static int eofflag;        /* end-of-file flag */
  20.  
  21. /*
  22.  * Open a file for reading.
  23.  */
  24. #if !(VMS & RMSIO)    /* if using RMS under VMS, the code */
  25.             /* is in VMS.C */
  26. PASCAL NEAR ffropen(fn)
  27. char    *fn;
  28. {
  29.         if ((ffp=fopen(fn, "r")) == NULL)
  30.                 return(FIOFNF);
  31.     eofflag = FALSE;
  32.         return(FIOSUC);
  33. }
  34.  
  35. /*
  36.  * Open a file for writing. Return TRUE if all is well, and FALSE on error
  37.  * (cannot create).
  38.  */
  39. #if    AOSVS == 0
  40. PASCAL NEAR ffwopen(fn, mode)
  41. char    *fn;
  42. char *mode;    /* mode to open file for */
  43. {
  44.     char xmode[6];        /* extended file open mode */
  45.  
  46.     /* nonstandard line terminators? */
  47.     if (*lterm) {
  48.  
  49.         /* open in binary mode */
  50.         strcpy(xmode, mode);
  51.         strcat(xmode, "b");
  52.         ffp = fopen(fn, xmode);
  53.     } else {
  54.  
  55.         /* open in ascii(text) mode */
  56.         ffp = fopen(fn, mode);
  57.     }
  58.  
  59.         if (ffp == NULL) {
  60.                 mlwrite(TEXT155);
  61. /*                      "Cannot open file for writing" */
  62.                 return(FIOERR);
  63.         }
  64.         return(FIOSUC);
  65. }
  66. #endif
  67.  
  68. /*
  69.  * Close a file. Should look at the status in all systems.
  70.  */
  71. PASCAL NEAR ffclose()
  72. {
  73.     /* free this since we do not need it anymore */
  74.     if (fline) {
  75.         free(fline);
  76.         fline = NULL;
  77.     }
  78.  
  79. #if    MSDOS & CTRLZ
  80.     putc(26, ffp);        /* add a ^Z at the end of the file */
  81. #endif
  82.     
  83. #if     WINNT | V7 | USG | SMOS | HPUX | SUN | XENIX | BSD | WMCS | VMS | (MSDOS & (LATTICE | MSC | DTL | TURBO | ZTC)) | OS2 | (TOS & MWC) | AVIION
  84.         if (fclose(ffp) != FALSE) {
  85.                 mlwrite(TEXT156);
  86. /*                      "Error closing file" */
  87.                 return(FIOERR);
  88.         }
  89.         return(FIOSUC);
  90. #else
  91.         fclose(ffp);
  92.         return(FIOSUC);
  93. #endif
  94. }
  95.  
  96. /*
  97.  * Write a line to the already opened file. The "buf" points to the buffer,
  98.  * and the "nbuf" is its length, less the free newline. Return the status.
  99.  * Check only at the newline.
  100.  */
  101. PASCAL NEAR ffputline(buf, nbuf)
  102.  
  103. char    buf[];
  104. int nbuf;
  105.  
  106. {
  107.         register int i;        /* index into line to write */
  108.     register char *lptr;    /* ptr into the line terminator */
  109. #if    CRYPT
  110.     char c;        /* character to translate */
  111.  
  112.     if (cryptflag) {
  113.             for (i = 0; i < nbuf; ++i) {
  114.             c = buf[i];
  115.             crypt(&c, 1);
  116.             putc(c, ffp);
  117.         }
  118.     } else
  119.             for (i = 0; i < nbuf; ++i)
  120.                     putc(buf[i], ffp);
  121. #else
  122.         for (i = 0; i < nbuf; ++i)
  123.                 putc(buf[i], ffp);
  124. #endif
  125.  
  126.     /* write out the appropriate line terminator(s) */
  127.     if (*lterm) {
  128.         lptr = <erm[0];
  129.         while (*lptr)
  130.             putc(*lptr++, ffp);
  131.     } else {
  132.             putc('\n', ffp);
  133.     }
  134.  
  135.     /* check for write errors */
  136.         if (ferror(ffp)) {
  137.                 mlwrite(TEXT157);
  138. /*                      "Write I/O error" */
  139.                 return(FIOERR);
  140.         }
  141.  
  142.         return(FIOSUC);
  143. }
  144.  
  145. /*
  146.  * Read a line from a file, and store the bytes in the supplied buffer. The
  147.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  148.  * at the end of the file that don't have a newline present. Check for I/O
  149.  * errors too. Return status.
  150.  */
  151. PASCAL NEAR ffgetline()
  152.  
  153. {
  154.         register int c;        /* current character read */
  155.         register int i;        /* current index into fline */
  156.  
  157.     /* if we are at the end...return it */
  158.     if (eofflag)
  159.         return(FIOEOF);
  160.  
  161.     /* dump fline if it ended up too big */
  162.     if (flen > NSTRING && fline != NULL) {
  163.         free(fline);
  164.         fline = NULL;
  165.     }
  166.  
  167.     /* if we don't have an fline, allocate one */
  168.     if (fline == NULL)
  169.         if ((fline = malloc(flen = NSTRING)) == NULL)
  170.             return(FIOMEM);
  171.  
  172.     /* read the line in */
  173.         i = 0;
  174.         while ((c = getc(ffp)) != EOF && c != '\n') {
  175.                 fline[i++] = c;
  176.         /* if it's longer, get more room */
  177.                 if (i >= flen) {
  178.             flen *= 2;
  179.             fline = realloc(fline, flen);
  180.                 }
  181.         }
  182.  
  183.     /* dump any extra line terminators at the end */
  184.     while (i > 0 && (fline[i-1] == 10 || fline[i-1] == 13))
  185.         i--;
  186.  
  187.     /* we should be ready to dump leading terminators too - ADD THIS DAN */
  188.  
  189.     /* test for any errors that may have occured */
  190.         if (c == EOF) {
  191.                 if (ferror(ffp)) {
  192.                         mlwrite(TEXT158);
  193. /*                              "File read error" */
  194.                         return(FIOERR);
  195.                 }
  196.  
  197.                 if (i != 0)
  198.             eofflag = TRUE;
  199.         else
  200.             return(FIOEOF);
  201.         }
  202.  
  203.     /* terminate and decrypt the string */
  204.         fline[i] = 0;
  205. #if    CRYPT
  206.     if (cryptflag)
  207.         crypt(fline, strlen(fline));
  208. #endif
  209.         return(FIOSUC);
  210. }
  211. #endif
  212.  
  213. int PASCAL NEAR fexist(fname)    /* does <fname> exist on disk? */
  214.  
  215. char *fname;        /* file to check for existance */
  216.  
  217. {
  218.     FILE *fp;
  219.  
  220.     /* try to open the file for reading */
  221.     fp = fopen(fname, "r");
  222.  
  223.     /* if it fails, just return false! */
  224.     if (fp == NULL)
  225.         return(FALSE);
  226.  
  227.     /* otherwise, close it and report true */
  228.     fclose(fp);
  229.     return(TRUE);
  230. }
  231.  
  232. #if    AZTEC & MSDOS
  233. /*    a1getc:        Get an ascii char from the file input stream
  234.             but DO NOT strip the high bit
  235. */
  236.  
  237. #undef    getc
  238.  
  239. int a1getc(fp)
  240.  
  241. FILE *fp;
  242.  
  243. {
  244.     int c;        /* translated character */
  245.  
  246.     c = getc(fp);    /* get the character */
  247.  
  248.     /* if its a <LF> char, throw it out  */
  249.     while (c == 10)
  250.         c = getc(fp);
  251.  
  252.     /* if its a <RETURN> char, change it to a LF */
  253.     if (c == '\r')
  254.         c = '\n';
  255.  
  256.     /* if its a ^Z, its an EOF */
  257.     if (c == 26)
  258.         c = EOF;
  259.  
  260.     return(c);
  261. }
  262. #endif
  263.